// app.js
App({
  globalData: {
    currentLevel: 1,
    bestScores: {},
    userInfo: null,
    soundEnabled: true
  },
  
  onLaunch() {
    // 加载本地存储的游戏数据
    this.loadGameData();
  },
  
  loadGameData() {
    try {
      const bestScores = wx.getStorageSync('bestScores') || {};
      const currentLevel = wx.getStorageSync('currentLevel') || 1;
      const soundEnabled = wx.getStorageSync('soundEnabled');
      
      this.globalData.bestScores = bestScores;
      this.globalData.currentLevel = currentLevel;
      this.globalData.soundEnabled = soundEnabled !== false;
    } catch (e) {
      console.log('加载游戏数据失败:', e);
    }
  },
  
  saveGameData() {
    try {
      wx.setStorageSync('bestScores', this.globalData.bestScores);
      wx.setStorageSync('currentLevel', this.globalData.currentLevel);
      wx.setStorageSync('soundEnabled', this.globalData.soundEnabled);
    } catch (e) {
      console.log('保存游戏数据失败:', e);
    }
  },
  
  updateBestScore(level, score) {
    if (!this.globalData.bestScores[level] || score > this.globalData.bestScores[level]) {
      this.globalData.bestScores[level] = score;
      this.saveGameData();
      return true;
    }
    return false;
  }
})